﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

using DNA;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using TJADRICD.CollaborativeDesignPlugin.Function;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Runtime.InteropServices;

namespace TJADRICD.CollaborativeDesignPlugin.Function
{
    public static class Function_PlotStyle
    {
        public static void UploadPlotStyle(String OldName, String NewName_Dwg, String Dir)
        {
            String DownLoadName = PlotStylePath();
            DirectoryInfo DownloadDir = new DirectoryInfo(DownLoadName);
            FileInfo File = new FileInfo(DownloadDir.FullName + @"\" + OldName);
            Tools.WriteMessageWithReturn(File.Name);
            if (!File.Exists)
            {
                MessageBox.Show("打印样式" + OldName + "未能找到");
                return;
            }

            bool flag = false;
            DirectoryInfo Dir_Config = new DirectoryInfo(Dir);
            DirectoryInfo Dir__Config = new DirectoryInfo(Dir_Config.FullName + @"\" + "Common");
            if (Dir__Config.Exists)
            {
                flag = true;
            }
            while (!flag && Dir_Config.Parent.Exists)
            {
                Dir_Config = Dir_Config.Parent;
                Dir__Config = new DirectoryInfo(Dir_Config.FullName + @"\" + "Common");
                if (Dir__Config.Exists)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                MessageBox.Show("无法找到Common文件夹");
                return;
            }
            DirectoryInfo UploadDir = new DirectoryInfo(Dir__Config.FullName + @"\PS");
            if (!UploadDir.Exists)
            {
                UploadDir.Create();
            }

            FileInfo dwgfile = new FileInfo(NewName_Dwg);

           

            String CTBNAME = dwgfile.Name.Substring(0, dwgfile.Name.Length - 4) + File.Name.Substring(File.Name.Length - 4, 4);

            FileInfo temp = new FileInfo(UploadDir.FullName + @"\" + CTBNAME);
            if (temp.Exists)
            {
                temp.Delete();
            }
            File.CopyTo(UploadDir.FullName + @"\" + CTBNAME);
        }

        public static void DownloadPlotStyle(String DwgFileName,String DirName)
        {
            FileInfo fi = new FileInfo(DwgFileName);
            DirectoryInfo di = fi.Directory;
            String CTBNAME = fi.Name.Substring(0, fi.Name.Length - 4) + ".ctb";

            bool flag = false;
            DirectoryInfo Dir_Config = new DirectoryInfo(DirName);
            DirectoryInfo Dir__Config = new DirectoryInfo(Dir_Config.FullName + @"\" + "Common");
            if (Dir__Config.Exists)
            {
                flag = true;
            }
            while (!flag && Dir_Config.Parent.Exists)
            {
                Dir_Config = Dir_Config.Parent;
                Dir__Config = new DirectoryInfo(Dir_Config.FullName + @"\" + "Common");
                if (Dir__Config.Exists)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                MessageBox.Show("无法找到Common文件夹");
                return;
            }
            DirectoryInfo UploadDir = new DirectoryInfo(Dir__Config.FullName + @"\PS");
            if (!UploadDir.Exists)
            {
                MessageBox.Show("PS目录不存在");
                UploadDir.Create();
            }

            String temp = PlotStylePath();
            DirectoryInfo DownloadDir = new DirectoryInfo(temp);
            FileInfo file = new FileInfo(UploadDir + @"\" + CTBNAME);
            if (!file.Exists)
            {
                MessageBox.Show("打印样式文件未上传");
                return;
            }
            FileInfo tf = new FileInfo(DownloadDir + @"\" + CTBNAME);
            if (tf.Exists)
            {
                tf.Delete();
            }
            file.CopyTo(DownloadDir + @"\" + "TJAD_PlotStyle.ctb",true);
        }

        public static String PlotStylePath()
        {
            Dictionary<String,String> options = Function_Registry.GetOptions();
            String path = options["PrinterStyleSheetDir"];
            return path;
        }
    }
}
